/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for SortLinksDropdown.
	/// </summary>
	public class SortLinksDropdown : DropDownList
	{
		public SortLinksDropdown()
		{
			
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if (Page.IsPostBack == false)
			{
				ArrayList sortOptions  =new ArrayList();

				sortOptions.Add(new ListItem("Link Name" , LinkViewSort.Name.ToString()));
				sortOptions.Add(new ListItem("Most Popular" ,  LinkViewSort.Popular.ToString()));
				sortOptions.Add(new ListItem("Newest" ,  LinkViewSort.Newest.ToString()));
				sortOptions.Add(new ListItem("Highest Rating" ,  LinkViewSort.Rating.ToString()));

				DataSource = sortOptions;
				DataValueField = "Value";
				DataTextField =  "Text";
				DataBind();
			}
		}

	}

	public class LinkSortingEventArgs : EventArgs 
	{
		public LinkViewSort SortOrder; 
	}

	public class LinkSorting : ResLinkBaseTemplatedWebControl
	{
		public delegate void SortOptionsChanged(object sender, LinkSortingEventArgs e);

		public event SortOptionsChanged SortChanged;

		private SortLinksDropdown _SortLinksDropdown = null;
		private Button _SortLinks = null;

		protected override void AttachChildControls()
		{
			_SortLinksDropdown = FindControl("LinkSortDrp") as  SortLinksDropdown;
			_SortLinks =  FindControl("SortLinks") as Button;

			_SortLinks.Click += new EventHandler(_SortLinks_Click);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if (!Page.IsPostBack)
			{
				DataBind();
			}
		}

		public override void DataBind()
		{
			base.DataBind();
		}

		private void _SortLinks_Click(object sender, EventArgs e)
		{
			//create a query that reflects the type of sorting
			if (SortChanged != null)
			{
				LinkSortingEventArgs args = new LinkSortingEventArgs();
				args.SortOrder = (LinkViewSort)Enum.Parse(typeof(LinkViewSort) , _SortLinksDropdown.SelectedValue);
				SortChanged(sender , args);
			}
		}
	}
}
